package com.wechat.service.storage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swak.annotation.FluxService;
import com.swak.entity.Page;
import com.swak.entity.Parameters;
import com.swak.entity.YesNo;
import com.swak.persistence.QueryCondition;
import com.swak.service.BaseService;
import com.swak.utils.StringUtils;
import com.wechat.dto.ChatDTO;
import com.wechat.dto.MessageDTO;
import com.wechat.entity.Message;
import com.wechat.enums.ContactType;
import com.wechat.enums.MessageFlag;
import com.wechat.msg.Msg;
import com.wechat.service.ChatService;
import com.wechat.service.MessageService;
import com.wechat.service.storage.mapper.MessageMapper;

/**
 * 描述
 * 
 * @author Auto Generator
 * @date 2022-10-21T23:59:27.286447600
 */
@FluxService
@Service("wechatMessageService")
public class MessageServiceImpl extends BaseService<Message, Long> implements MessageService {

	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private ChatService chatService;

	/**
	 * 直取
	 */
	@Override
	public MessageDTO get(Long id) {
		return MessageDTO.convert(this.messageMapper.get(id));
	}

	@Override
	@Transactional
	public void deleteByChat(String talker) {
		this.messageMapper.deleteSelective(Message.of().setTalker(talker));
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional
	public Integer delete(MessageDTO message) {
		Message entity = message.transform();
		return this.messageMapper.delete(entity);
	}

	/**
	 * 分页查询
	 */
	@Override
	public Page<MessageDTO> page(MessageDTO query, Parameters param) {
		QueryCondition qc = new QueryCondition();
		qc.getCriteria().andEqualTo(Message.DelFlag, Boolean.FALSE);
		if (StringUtils.isNotBlank(query.getTalker())) {
			qc.getCriteria().andEqualTo(Message.Talker, query.getTalker());
		}
		if (query.getId() != null) {
			qc.getCriteria().andLessThan(Message.Id, query.getId());
		}
		qc.setOrderByClause(param.orderBy(Message.DefaultOrderBy));
		return MessageDTO.convert(this.messageMapper.queryForPage(qc, param));
	}

	@Override
	@Transactional
	public MessageDTO prepare(MessageDTO message) {

		// 保存最新的消息
		Message saved = message.transform()// 转换基本信息
				.setIsSender(YesNo.Yes.getType()) // 我发送的数据
				.setMsgFlag(MessageFlag.Unsend.getFlag())// 消息状态 -- 默认是未发送
				.setSequence(0L); // 发送消息不需要记录序号
		this.messageMapper.insert(saved);

		// 保存聊天信息
		ChatDTO chat = ChatDTO.of()// 实例化
				.setId(saved.getTalker())// 聊天ID
				.setLastMsgContent(saved.getMsgBrief())// 最后的聊天消息 - 简述
				.setLastMsgDate(saved.getCreateDate())// 最后的聊天消息
				.setLastMsgType(saved.getMsgType())// 最后的聊天消息
				.setLastMsgId(saved.getId());// 最后的聊天消息
		this.chatService.updateMessage(chat);

		// 返回消息
		return this.get(saved.getId());
	}

	@Override
	@Transactional
	public MessageDTO confirm(MessageDTO message) {
		Message.of().setId(message.getId()).setMsgFlag(message.getMsgFlag()).updateSelective();
		return message;
	}

	@Override
	@Transactional
	@SuppressWarnings("rawtypes")
	public MessageDTO receive(MessageDTO message, Msg content) {

		// 设置收到消息的序列号
		Long sequence = this.chatService.receiveMessage(ChatDTO.of()// 实例化
				.setId(message.getTalker())// 设置消息的发送人
				.setType(ContactType.of(message.getTalker()).getType())// 设置类型
				.setNickName(content.getNickName()));

		// 保存最新的消息
		Message saved = message.transform()// 转换基本信息
				.setIsSender(YesNo.No.getType()) // 我发送的数据
				.setMsgFlag(MessageFlag.Recved.getFlag())// 消息状态 -- 接收成功
				.setSequence(sequence); // 发送消息不需要记录序号
		this.messageMapper.insert(saved);

		// 保存聊天信息
		this.chatService.updateMessage(ChatDTO.of()// 实例化
				.setId(saved.getTalker())// 聊天ID
				.setLastMsgContent(saved.getMsgBrief())// 最后的聊天消息 - 简述
				.setLastMsgDate(saved.getCreateDate())// 最后的聊天消息
				.setLastMsgType(saved.getMsgType())// 最后的聊天消息
				.setLastMsgId(saved.getId()));// 最后的聊天消息

		// 返回消息
		return this.get(saved.getId());
	}
}
